package com.teleca.jamendo.ui.widget;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.teleca.jamendo.R;
import com.teleca.jamendo.util.StringUtil;

public class LoadingUpView {
	private static final String TAG = "LoadingUpView";
	// 限制用户操作时间
	private static final int Default_DialogTimeOut = 300;
	private static final int SRART_ANIM = 3;
	private static final int ERROR = 4;
	private static final int DISMISS = 5;
	private static final int SHOW_DIALOG = 6;
	private static final int Handler_TimeTask_Timeout = 7;
	private int DialogTimeOut = Default_DialogTimeOut;
	private LinearLayout mLoadingView;
	private Animation mAnim;
	private ImageView mIvLoading;
	private TextView mTvMsg;
	private Activity mCurrentActivity;
	private Boolean mIsShowing = false;
	private View mParentView;
	private ProgressDialog mProgressdDialog;
	private boolean mIsFrameLayoutParentView;

	private int mScreenWidth;
	private int mScreenHeight;
	private int mStatusbarHeight;
	// 是否为限制用户操作view，为透明，显示后一定时间自动隐藏
	private boolean mIsLimitView;

	private DialogTimeTask mDialogTimeTask;

	// 是否阻塞用户操作
	private boolean mIsBlock;

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case SRART_ANIM:
					if (mIvLoading != null) {
						mIvLoading.startAnimation(mAnim);
						// 获取焦点，阻塞用户操作
						// if (mIsBlock) {
						// mParentView.setFocusable(true);
						// mParentView.setFocusableInTouchMode(true);
						// mLoadingView.requestFocus();
						// mLoadingView.setOnClickListener(new OnClickListener()
						// {
						// @Override
						// public void onClick(View v) {
						// }
						// });
						// }
					}
					break;
				case ERROR:
					break;
				case DISMISS:
					if (mIsBlock) {
						if (mProgressdDialog != null) {
							mProgressdDialog.dismiss();
							mProgressdDialog = null;
						}
					} else {
						removeLoadingView();
						mLoadingView = null;
					}
					break;
				case SHOW_DIALOG:
					showProgressDialog(mCurrentActivity, String.valueOf(msg.obj));
					break;
				case Handler_TimeTask_Timeout:
					if (mProgressdDialog != null) {
						mProgressdDialog.dismiss();
						mProgressdDialog = null;
					}
					cancelTimeTask();
					break;
				default:
					break;
			}
		}
	};

	/**
	 * 构造方法
	 * 
	 * @param act
	 *            Activity
	 * @param isBlock
	 *            是否阻塞用户操作
	 */
	public LoadingUpView(Activity act, boolean isBlock) {
		this.mIsBlock = isBlock;
		init(act);
	}

	/**
	 * 构造方法
	 * 
	 * @param act
	 *            Activity
	 */
	public LoadingUpView(Activity act) {
		init(act);
	}

	/**
	 * 
	 * setmIsLimitView 是否是显示限制用户点击view
	 * 
	 * @param mIsLimitView
	 * @return void
	 */
	public void setmIsLimitView(boolean mIsLimitView) {
		this.mIsLimitView = mIsLimitView;
		this.mIsBlock = mIsLimitView;
	}

	public void setmIsLimitAndShowTime(boolean mIsLimitView, int dialogTimeOut) {
		this.mIsLimitView = mIsLimitView;
		this.mIsBlock = mIsLimitView;
		this.DialogTimeOut = dialogTimeOut;
	}

	/**
	 * 初始化
	 * 
	 * @param act
	 *            Activity
	 */
	private void init(Activity act) {
		this.mCurrentActivity = act;
		if (act.getWindow() == null) {
			return;
		}
		mParentView = act.getWindow().getDecorView().getRootView();
		if (mParentView instanceof FrameLayout) {
			mIsFrameLayoutParentView = true;
		} else {
			mIsFrameLayoutParentView = false;
		}
	}

	private void initView() {
		LayoutInflater inflater = (LayoutInflater) mCurrentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLoadingView = (LinearLayout) inflater.inflate(R.layout.popup, null);
		mTvMsg = (TextView) mLoadingView.findViewById(R.id.tv_popup);
		mIvLoading = (ImageView) mLoadingView.findViewById(R.id.img_popup);
		mAnim = AnimationUtils.loadAnimation(mCurrentActivity, R.anim.popup_loading);
	}

	/**
	 * 显示“加载中...”
	 * 
	 */
	public void showPopup() {
		showPopup(mCurrentActivity.getString(R.string.loading));
	}

	/**
	 * show EvtLoadingUpView
	 * 
	 * @param msg
	 *            内容
	 */
	public void showPopup(final String msg) {
		synchronized (mIsShowing) {
			// 阻塞
			if (mIsBlock) {
				if (mIsLimitView) {
					setTimeTask();
				}
				changeShowStatus(true);
				Message mMsg = new Message();
				mMsg.what = SHOW_DIALOG;
				mMsg.obj = msg;
				mHandler.sendMessage(mMsg);
			} else {
				if (mParentView == null) {
					return;
				}
				if (mLoadingView == null) {
					initView();
				}
				removeLoadingView();
				addLoadingView(msg);
			}
		}

	}

	/**
	 * dismiss EvtLoadingUpView
	 */
	public void dismiss() {
		synchronized (mIsShowing) {
			if (mIsShowing) {
				changeShowStatus(false);
				mHandler.sendEmptyMessage(DISMISS);
			}
		}
	}

	/**
	 * 
	 * 设置是否阻塞用户操作，在使用showPopup方法前调用
	 * 
	 * @param isBlock
	 *            是否阻塞用户操作
	 */
	public void setIsBlock(boolean isBlock) {
		this.mIsBlock = isBlock;
	}

	private void removeLoadingView() {
		if (mIsFrameLayoutParentView) {
			((FrameLayout) mParentView).removeView(mLoadingView);
		}
	}

	private void addLoadingView(String msg) {
		if (mIsFrameLayoutParentView) {
			if (mScreenWidth == 0) {
				getDisplayConfig();
			}
			((FrameLayout) mParentView).addView(mLoadingView, new FrameLayout.LayoutParams(mScreenWidth, mScreenHeight
					- mStatusbarHeight));
			if (StringUtil.isNullOrEmpty(msg)) {
				mTvMsg.setVisibility(View.GONE);
			} else {
				mTvMsg.setVisibility(View.VISIBLE);
				mTvMsg.setText(msg);
			}
			changeShowStatus(true);
			mHandler.sendEmptyMessage(SRART_ANIM);
		}
	}

	/**
	 * onResume 非阻塞式 时需使用
	 * 
	 * @Description: 保持loadingview状态 在Activity onResume() 方法中调用
	 */
	public void onResume() {
		if (mIsShowing && mLoadingView != null) {
			mHandler.sendEmptyMessage(SRART_ANIM);
		}
	}

	/**
	 * 返回EvtLoadingUpView是否显示
	 * 
	 * @return EvtLoadingUpView是否显示
	 */
	public boolean isShowing() {
		return mIsShowing;
	}

	private void changeShowStatus(boolean bShowing) {
		this.mIsShowing = bShowing;
	}

	/**
	 * showProgressDialog 创建显示
	 * 
	 * @param act
	 *            Activity
	 * @param msg
	 *            消息
	 */
	public void showProgressDialog(Activity act, String msg) {
		Activity activity = act;
		if (act.isFinishing()) {
			return;
		}
		if (act.getParent() != null) {
			activity = act.getParent();
		}
		if (activity.getParent() != null) {
			activity = activity.getParent();
		}
		if (mProgressdDialog == null) {
			mProgressdDialog = new ProgressDialog(activity);
			mProgressdDialog.show();
			LayoutInflater mLayoutInflater = LayoutInflater.from(activity);
			View view = mLayoutInflater.inflate(R.layout.popup, null);
			ImageView mRetote = (ImageView) view.findViewById(R.id.img_popup);
			TextView mLoadMsg = (TextView) view.findViewById(R.id.tv_popup);

			if (!mIsLimitView) {
				mLoadMsg.setVisibility(View.VISIBLE);
				mLoadMsg.setVisibility(View.VISIBLE);
				if (mAnim == null) {
					mAnim = AnimationUtils.loadAnimation(mCurrentActivity, R.anim.popup_loading);
				}
				mAnim.setInterpolator(new LinearInterpolator());
				mRetote.startAnimation(mAnim);

				mLoadMsg.setText(msg);
			}
			Window window = mProgressdDialog.getWindow();
			WindowManager.LayoutParams lp = window.getAttributes();
			lp.alpha = 1f;
			lp.dimAmount = 0.000001f; // 修改在2.1和2.2的模拟器上黑屏的问题
			window.setAttributes(lp);
			window.setContentView(view);

			mProgressdDialog.setIndeterminate(true);
			mProgressdDialog.setCancelable(false);
		}
	}

	/**
	 * 超时计时器
	 */
	private void setTimeTask() {
		cancelTimeTask();
		Timer mTimer = new Timer();
		mDialogTimeTask = new DialogTimeTask();
		mTimer.schedule(mDialogTimeTask, DialogTimeOut);
	}

	private void cancelTimeTask() {
		if (mDialogTimeTask != null) {
			mDialogTimeTask.cancel();
			mDialogTimeTask = null;
		}
	}

	class DialogTimeTask extends TimerTask {

		/**
		 * run
		 * 
		 * @see java.util.TimerTask#run()
		 */
		@Override
		public void run() {
			Message msg = mHandler.obtainMessage(Handler_TimeTask_Timeout);
			mHandler.sendMessage(msg);
		}
	}

	/*
	 * 获取分辨率
	 */
	private void getDisplayConfig() {

		DisplayMetrics dm = new DisplayMetrics();
		((Activity) mCurrentActivity).getWindowManager().getDefaultDisplay().getMetrics(dm);
		// 宽度
		mScreenWidth = dm.widthPixels;
		mScreenHeight = dm.heightPixels;
		Rect frame = new Rect();
		mCurrentActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		mStatusbarHeight = frame.top;

	}
}
